package com.ax.base.drugadministration.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.drugadministration.domain.BaseDrugDep;
import com.ax.base.drugadministration.domain.BaseDrugManager;
import com.ax.base.drugadministration.service.DrugAdministrationDepService;
import com.ax.base.drugadministration.service.DrugAdministrationManagerService;
import com.ax.base.system.domain.MenuDO;

@Controller
@RequestMapping("/sys/drugAdministration")
public class DrugAdministrationDepController {
	@Autowired
	DrugAdministrationDepService drugAdministrationService;
	@Autowired
	DrugAdministrationManagerService drugAdministrationManagerService;
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String drugAdministration(Model model) {
		return "drugAdministration/department/drugAdministration";
	}
	
	/**
	 * 药监查询(模块:药监部门)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseDrugDep>
	 */
	@RequestMapping("/queryDrugAdministrationDep")
	@ResponseBody
	public List<BaseDrugDep> queryDrugAdministrationDep(@RequestParam Map<String, Object> params) {
		   List<BaseDrugDep> drugAdministrations = this.drugAdministrationService.queryDrugAdministrationDepSer(params);
		   return drugAdministrations;
	  }
	//yige
	/**
	 * 药监增加(模块:药监部门)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseDrugDep baseDrugDep
	 */
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName",drugAdministrationService.getBaseDrugDep(pId).getName());
		}
		return "drugAdministration/department/add";
	}
	
	/**
	 * 药监保存(模块:药监部门)
	 * 
	 * @param BaseDrugDep baseDrugDep
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertDrugDep")
	@ResponseBody
	public AjaxRes insertDrugDep(AjaxRes ajaxRes,BaseDrugDep baseDrugDep){
		try {
			this.drugAdministrationService.insertDrugDepSer(baseDrugDep);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/*@PostMapping("/insertDrugDepManager")
	@ResponseBody
	public AjaxRes insertDrugManager(AjaxRes ajaxRes,String phone){
		try {
			BaseDrugManager baseDrugManager = new BaseDrugManager();
			this.drugAdministrationManagerService.insertDrugManagerSer(baseDrugManager);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}*/
	
	/**
	 * 药监修改(模块:药监部门)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return BaseDrugDep baseDrugDep
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("dep",drugAdministrationService.getBaseDrugDep(id));
		return "drugAdministration/department/edit";
	}
	
	/**
	 * 药监修改(模块:药监部门)
	 * 
	 * @param BaseDrugDep baseDrugDep
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateDrugDep")
	@ResponseBody
	public AjaxRes updateDrugDep(AjaxRes ajaxRes,BaseDrugDep baseDrugDep){
		try {
			this.drugAdministrationService.updateDrugDepSer(baseDrugDep);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 药监状态修改(模块:药监部门)
	 * 
	 * @param String[] parameter,Integer flag
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateState")
	@ResponseBody
	public AjaxRes updateState(AjaxRes ajaxRes,@RequestParam("parameter[]")String[] parameter,Integer flag){
		try {
			this.drugAdministrationService.updateStateSer(parameter,flag);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 树形列表(药监部门)
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@RequestMapping("/list")
	@ResponseBody
	Tree<MenuDO> list(){
		Tree<MenuDO> BaseDeps = drugAdministrationService.list();
		return BaseDeps;
	}
	
	/**
	 * 编辑修改树
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@RequestMapping("/list/{drugDepId}")
	@ResponseBody
	Tree<MenuDO> arealist(@PathVariable("drugDepId") Long drugDepId){
		Tree<MenuDO> BaseDeps = drugAdministrationService.treeList(drugDepId);
		return BaseDeps;
	}
	
	/**
	 * Tree
	 * 
	 * @param Long parentDrugDepId
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree/{parentDrugDepId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("parentDrugDepId") Long parentDrugDepId) {
		Tree<MenuDO> tree = drugAdministrationService.getDrugTree(parentDrugDepId);
		return tree;
	}
	
	/**
	 * 名字是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/validate")
	@ResponseBody
	public boolean validate(String parameter){
		return !drugAdministrationService.validateSer(parameter);
	}
	
	/**
	 * 名字是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/updateValidate")
	@ResponseBody
	public boolean updateValidate(String parameter){
		return !drugAdministrationService.updateValidateSer(parameter);
	}
}
	
	
	
